const controller = getAjaxBaseUrl('/emissionFactors', '/energy')

export interface EmissionFactorsQueryParams {
  pageNumber: number
  pageSize: number
  energyName?: string
  startTime?: string
}

export interface EmissionFactorBody {
  energyCode: string
  energyName: string
  startTime: string
  emissionFactors: string | number
  unit: string
  [key: string]: any
}

/** 分页查询碳排放因子列表 */
export function queryList(params: EmissionFactorsQueryParams) {
  return post<{
    records: EmissionFactorBody[]
    total: number
  }>(`${controller}/page`, { ...params })
}

/** 新增或编辑碳排放因子 */
export function saveOrUpdate(params: EmissionFactorBody) {
  return post(`${controller}/saveOrUpdate`, params)
}

/** 删除碳排放因子 */
export function deleteByIds(ids: number[]) {
  return post(`${controller}/delete`, ids)
}

/** 下载碳排放因子模板 */
export async function downloadTemplate() {
  const { data } = await request({
    url: `${controller}/downloadTemplate`,
    method: 'post',
    responseType: 'blob',
  })

  downloadByA(
    URL.createObjectURL(data),
    `碳排放因子模板_${formatTime(
      Date.now(),
      'YYYY-MM-DD_hh-mm-ss',
    )}`,
  )
}

/** 批量导入碳排放因子 */
export function importExcel(file: File) {
  const params = new FormData()
  params.append('file', file)
  return post(`${controller}/import`, params)
}

const c = getAjaxBaseUrl('/energyCarbon', '/energy')
/** 查询碳排放基表信息 */
export function getList() {
  return get<EmissionFactorBody[]>(`${c}/getList`)
}
